package com.tueye.web.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestMapping;

import com.tueye.ajax.json.JSONArray;
import com.tueye.ajax.json.JSONException;
import com.tueye.ajax.json.JSONObject;
import com.tueye.base.util.Page;
import com.tueye.entity.Topics;
import com.tueye.service.ITopicService;
import com.tueye.service.exception.TuEyeServiceException;

import freemarker.template.TemplateException;

@SuppressWarnings("unchecked")
@org.springframework.stereotype.Controller
@RequestMapping("/topics.do")
public class TopicsController {
	
	@Autowired
	@Qualifier("topicService")
	ITopicService topicService;
	
	private JSONObject getTopicJson (List list)
	{
		JSONObject json = new JSONObject();
		JSONArray members = new JSONArray();
		try {
			if(list.size() > 0){
				for(int i = 0 ; i < list.size() ; i++ ){
					Topics topic = (Topics) list.get(i);
					JSONObject member = new JSONObject()
						.put("topicId", topic.getId())
						.put("articleTopicId", topic.getArticleTopic().getId())
						.put("topicTypeId", topic.getTopic_type().getId())
						.put("topicTypeEn", topic.getTopic_type().getType_enlish_name())
						.put("topicTitle", topic.getTopic_title())
						.put("date", "'"+topic.getSys_time());
					members.put(i, member);
				}
			}
			json.put("topics", members);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return json ;
	}
	// add by shiwei 091123
	@RequestMapping(params = "action=topicByArttype") 
	public String  getTopicByArttype(HttpServletRequest request, HttpServletResponse response) throws TuEyeServiceException, IOException, TemplateException{
		
		String keywords = request.getParameter("keywords");
		String ariticeTopicId = request.getParameter("ariticeTopicId");
		//同专题文章
		StringBuffer sameTypeHql = new StringBuffer(" from com.tueye.entity.Topics pics where ");
		if(keywords != null && !"".equals(keywords))
			sameTypeHql.append(" pics.keywords='" + keywords + "'   and ");
		if(ariticeTopicId != null && !"".equals(ariticeTopicId))
			sameTypeHql.append(" pics.articleTopic.id='" + ariticeTopicId + "'   and ");
		sameTypeHql.delete(sameTypeHql.length()-6, sameTypeHql.length());
		//Page sameTypePage = topicService.pagedQueryByHQL(sameTypeHql.toString(), new Integer(pageString), new Integer(rp), null);
		Page sameTypePage = topicService.pagedQueryByHQL(sameTypeHql.toString(),1,  Integer.valueOf("20"), null);
		
		JSONObject json = getTopicJson((ArrayList)sameTypePage.getResult());
		System.out.println("jsonStr:" + json.toString());
		response.setContentType("html/txt");
		response.setCharacterEncoding("utf-8");
		response.getWriter().write(json.toString());
		return null ;
	}
	// add by shiwei 091123
	@RequestMapping(params = "action=getHotTopic") 
	public String  getHotTopic(HttpServletRequest request, HttpServletResponse response) throws TuEyeServiceException, IOException, TemplateException{
		String qtype=request.getParameter("qtype");//获取 查询的类型
		String ariticeTopicId = request.getParameter("qtype");
		//编辑推荐
		StringBuffer heigtClickHql = new StringBuffer(" from com.tueye.entity.Topics pics where ");
		//可以加条件判断
		if(ariticeTopicId != null && !"".equals(ariticeTopicId))
			heigtClickHql.append(" pics.articleTopic.id ='" + ariticeTopicId +"'   and ");
		heigtClickHql.delete(heigtClickHql.length()-6, heigtClickHql.length());
		heigtClickHql.append(" order by pics.topic_clickCount desc ");
		//Page heigtClickPage = topicService.pagedQueryByHQL(heigtClickHql.toString(), new Integer(pageString), new Integer(rp), null);
		Page heigtClickPage = topicService.pagedQueryByHQL(heigtClickHql.toString(),1,  Integer.valueOf("12"), null);
		
		JSONObject json = getTopicJson((ArrayList)heigtClickPage.getResult());
		System.out.println("jsonStr:" + json.toString());
		response.setContentType("html/txt");
		response.setCharacterEncoding("utf-8");
		response.getWriter().write(json.toString());
		return null ;
	}
	// add by shiwei 091123
	@RequestMapping(params = "action=getWeekTopic") 
	public String  getWeekTopic(HttpServletRequest request, HttpServletResponse response) throws TuEyeServiceException, IOException, TemplateException{
		String qtype=request.getParameter("qtype");//获取 查询的类型
		String ariticeTopicId = request.getParameter("qtype");
		//本周推荐
		StringBuffer sameWeekHql = new StringBuffer(" from com.tueye.entity.Topics pics where ");
		//可以加条件判断
		sameWeekHql.delete(sameWeekHql.length()-6, sameWeekHql.length());
		sameWeekHql.append(" order by pics.topic_clickCount desc ");
		//Page sameWeekPage = topicService.pagedQueryByHQL(sameWeekHql.toString(), new Integer(pageString), new Integer(rp), null);
		Page sameWeekPage = topicService.pagedQueryByHQL(sameWeekHql.toString(), 1,  Integer.valueOf("12"), null);
		
		JSONObject json = getTopicJson((ArrayList)sameWeekPage.getResult());
		System.out.println("jsonStr:" + json.toString());
		response.setContentType("html/txt");
		response.setCharacterEncoding("utf-8");
		response.getWriter().write(json.toString());
		return null ;
	}
	//
	@RequestMapping(params = "action=getClickNums") 
	public String  getClickNums(HttpServletRequest request, HttpServletResponse response) throws TuEyeServiceException, IOException, TemplateException{
		String topicid=request.getParameter("topicId");//获取 查询的类型
		String articleTopicId = request.getParameter("articleTopicId");
		String clickNums =  topicService.getClickNumByTopicId(topicid) ;
		//JSONObject json = null; 
		JSONObject json = new JSONObject();
		try{
			json.put("clickNums", clickNums) ;
			System.out.println("clickNums：" + clickNums);
		}catch(Exception e)
		{
			System.out.println("error：封装点击数出错！");
		}
		System.out.println("jsonStr:" + json.toString());
		response.setContentType("html/txt");
		response.setCharacterEncoding("utf-8");
		response.getWriter().write(json.toString());
		return null ;
	}
}
